import 'package:flutter/material.dart';
import 'package:shirne_dialog/shirne_dialog.dart';

import '../../application.dart';
import '../../utils/api.dart';
import '../../models/member_address_model.dart';
import '../../widgets/list_state_page.dart';

class AddressesPage extends StatefulWidget {
  const AddressesPage({Key? key}) : super(key: key);

  @override
  State<AddressesPage> createState() => _AddressesPageState();
}

class _AddressesPageState
    extends ListStatePage<AddressesPage, MemberAddressModel> {
  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的地址'),
        actions: [
          TextButton.icon(
            onPressed: () {
              app.route('/member/address/detail');
            },
            icon: Icon(
              Icons.add,
              color: colorScheme.onPrimary,
            ),
            label: Text(
              '添加',
              style: TextStyle(color: colorScheme.onPrimary),
            ),
          ),
          const SizedBox(width: 10),
        ],
      ),
      body: SafeArea(
        child: buildListView(),
      ),
    );
  }

  @override
  String get apiUrl => 'member.address/index';

  @override
  Widget buildItem(MemberAddressModel item) {
    final colorScheme = Theme.of(context).colorScheme;
    return Container(
      margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
      padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 0),
      decoration: BoxDecoration(
        color: colorScheme.surface,
        borderRadius: const BorderRadius.all(Radius.circular(5)),
      ),
      child: ListTile(
        leading: CircleAvatar(
          child: Text(item.receiveName.substring(0, 1)),
        ),
        title: RichText(
          text: TextSpan(
            style: TextStyle(color: colorScheme.onSurface.withAlpha(200)),
            children: [
              TextSpan(
                  text: item.receiveName,
                  style: TextStyle(color: colorScheme.onSurface, fontSize: 16)),
              const TextSpan(text: ' '),
              TextSpan(text: item.mobile),
              if (item.isDefault)
                WidgetSpan(
                  child: Container(
                    margin: const EdgeInsets.only(left: 5),
                    padding:
                        const EdgeInsets.symmetric(vertical: 0, horizontal: 10),
                    decoration: const BoxDecoration(
                      color: Color.fromRGBO(255, 232, 232, 1),
                      borderRadius: BorderRadius.all(Radius.circular(20)),
                    ),
                    child: const Text(
                      '默认',
                      style: TextStyle(color: Colors.red, fontSize: 12),
                    ),
                  ),
                ),
            ],
          ),
        ),
        subtitle: Text(
          '${item.areaView} ${item.address}',
          style: TextStyle(color: colorScheme.onSurface.withAlpha(150)),
        ),
        trailing: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            IconButton(
              onPressed: () {
                app.route('/member/address/detail',
                    arguments: {'id': item.id, 'item': item});
              },
              icon: const Icon(Icons.mode_edit, size: 18),
            ),
            IconButton(
              onPressed: () async {
                final confirm = await MyDialog.of(context).confirm('确定删除？');
                if (!(confirm ?? false)) {
                  return;
                }
                final result =
                    await Api.post('member.address/delete', {'id': item.id});
                if (result.code == 1) {
                  MyDialog.of(context).toast(result.message);
                  reloadData();
                } else {
                  MyDialog.of(context)
                      .toast(result.message, icon: MyDialog.iconWarning);
                }
              },
              icon: const Icon(
                Icons.delete_rounded,
                size: 18,
                color: Colors.red,
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  MemberAddressModel transItem(item) {
    return MemberAddressModel.fromJson(item);
  }
}
